package com.parking.certified.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parking.certified.client.inter.StallClient;
import com.parking.certified.dao.CertifiedInfoMapper;
import com.parking.certified.dao.CertifiedLoginLogsMapper;
import com.parking.certified.dto.CertifiedAccountClientDto;
import com.parking.certified.dto.CertifiedAccountDTO;
import com.parking.certified.dto.CertifiedDto;
import com.parking.certified.entity.CertifiedAccount;
import com.parking.certified.dao.CertifiedAccountMapper;
import com.parking.certified.entity.CertifiedInfo;
import com.parking.certified.entity.CertifiedLoginLogs;
import com.parking.certified.exception.CertifiedException;
import com.parking.certified.exception.CertifiedExceptionCode;
import com.parking.certified.form.AddCertifiedForm;
import com.parking.certified.form.CertifiedLoginForm;
import com.parking.certified.form.UpdateStateForm;
import com.parking.certified.service.CertifiedAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.certified.service.config.AddressLocation;
import com.parking.certified.service.config.MapUtil;
import com.parking.common.core.model.Result;
import com.parking.common.core.model.StateData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 彭于晏
 * @since 2023年11月29日
 */
@Service
public class CertifiedAccountServiceImpl extends ServiceImpl<CertifiedAccountMapper, CertifiedAccount> implements CertifiedAccountService {
    @Resource
    private CertifiedAccountMapper certifiedAccountMapper;
    @Resource
    private StallClient stallClient;
    @Resource
    private CertifiedInfoMapper certifiedInfoMapper;
    @Resource
    private CertifiedLoginLogsMapper certifiedLoginLogsMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;

    /***
     * 平台添加物业账号
     * @param form
     * @param adminId
     */
    @Override
    public void addCertified(AddCertifiedForm form, Integer adminId)throws Exception {
        QueryWrapper<CertifiedAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("certified_account_account",form.getCertifiedAccountAccount());
        CertifiedAccount account = certifiedAccountMapper.selectOne(wrapper);
        if(account!=null){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NOT_NULL);
        }
        QueryWrapper<CertifiedInfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("certified_info_address",form.getCertifiedInfoAddress());
        CertifiedInfo info = certifiedInfoMapper.selectOne(wrapper1);
        if (info!=null){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_ADDRESS_NOT_NULL);
        }
        CertifiedAccount certifiedAccount = BeanUtil.toBean(form, CertifiedAccount.class);
        certifiedAccount.setCertifiedAccountUpdateTime(LocalDateTime.now());
        certifiedAccount.setCertifiedAccountAddPerson(String.valueOf(adminId));
        certifiedAccount.setCertifiedAccountUpdatePerson(String.valueOf(adminId));
        certifiedAccount.setCertifiedAccountAddTime(String.valueOf(LocalDateTime.now()));
        certifiedAccount.setCertifiedAccountState(StateData.CERTIFIED_STATE_ENABLE);
        certifiedAccount.setAccountRole(StateData.ROLE_STATE_CERTIFIED);
        certifiedAccountMapper.insert(certifiedAccount);
        CertifiedInfo certifiedInfo = BeanUtil.toBean(form, CertifiedInfo.class);
        certifiedInfo.setCertifiedInfoName(form.getCertifiedInfoName());
        certifiedInfo.setCertifiedInfoAddPerson(String.valueOf(adminId));
        certifiedInfo.setCertifiedInfoScore(BigDecimal.ZERO);
        certifiedInfo.setChargeUnitPrice(BigDecimal.ZERO);
        certifiedInfo.setStallChargeNum(0);
        certifiedInfo.setStallCommonNum(0);
        certifiedInfo.setStallUnitPrice(BigDecimal.ZERO);
        certifiedInfo.setCertifiedInfoTel("66666666");
        certifiedInfo.setCertifiedInfoAddTime(LocalDateTime.now());
        certifiedInfo.setCertifiedInfoAddress(form.getCertifiedInfoAddress());
        AddressLocation addressLocation = MapUtil.getAddressLocation(form.getCertifiedInfoAddress());
        certifiedInfo.setCertifiedInfoLatitude(addressLocation.getLatitude());
        certifiedInfo.setCertifiedInfoLongitude(addressLocation.getLongitude());
        certifiedInfo.setCertifiedAccountId(certifiedAccount.getCertifiedAccountId());
        certifiedInfoMapper.insert(certifiedInfo);
        //消息队列修改日志信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("certifiedAccount",certifiedAccount);
        rabbitTemplate.convertAndSend("addCertified",map);
    }

    /***
     * 物业登录
     * @param form
     * @param response
     * @return
     */
    @Override
    public CertifiedAccountDTO certifiedLogin(CertifiedLoginForm form, HttpServletResponse response) {
        QueryWrapper<CertifiedAccount> wrapper = new QueryWrapper<>();
        wrapper.like("certified_account_account",form.getCertifiedAccountAccount());
        wrapper.like("certified_account_pass",form.getCertifiedAccountPass());
        CertifiedAccount account = certifiedAccountMapper.selectOne(wrapper);
        if (account==null){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NULL);
        }
        if (account.getCertifiedAccountState()!=StateData.CERTIFIED_STATE_ENABLE){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NOT_ENABLE);
        }
        if (account.getAccountRole()!=StateData.ROLE_STATE_CERTIFIED){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_ROLE_IS_NOT);
        }
        CertifiedAccountDTO dto = BeanUtil.toBean(account, CertifiedAccountDTO.class);
        //消息队列
        CertifiedLoginLogs logs = new CertifiedLoginLogs();
        logs.setCertifiedLoginAccount(dto.getCertifiedAccountAccount());
        logs.setCertifiedLoginTime(LocalDateTime.now());
        logs.setCertifiedAccountId(dto.getCertifiedAccountId());
        //创建map集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("certifiedLoginLogs",logs);
        //使用消息队列
        rabbitTemplate.convertAndSend("certifiedLogin",map);
        return dto;
    }

    /***
     * 退出登录
     * @param certifiedAccountId
     */
    @Override
    public void loginOut(Integer certifiedAccountId) {
        CertifiedLoginLogs logs = certifiedLoginLogsMapper.selectById(certifiedAccountId);
        logs.setCertifiedLoginState(StateData.CERTIFIED_LOGS_ISNOT_LOGIN);
    }

    /***
     * 平台查询物业账号
     * @param certifiedAccountId
     * @return
     */
    @Override
    public List<CertifiedAccountDTO> selectCertifiedAccount(Integer certifiedAccountId,Integer platformId) {
        QueryWrapper<CertifiedAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("certified_account_id",certifiedAccountId);
        List<CertifiedAccount> list = certifiedAccountMapper.selectList(wrapper);
        ArrayList<CertifiedAccountDTO> dtos = new ArrayList<>();
        for (CertifiedAccount certifiedAccount : list) {
            CertifiedAccountDTO dto = BeanUtil.toBean(certifiedAccount, CertifiedAccountDTO.class);
            dtos.add(dto);
        }
        return dtos;
    }

    /***
     * 平台启用物业账号
     * @param certifiedAccountId
     * @param platformId
     */
    @Override
    public void enableCertifiedState(Integer certifiedAccountId,Integer platformId) {
        CertifiedAccount account = certifiedAccountMapper.selectById(certifiedAccountId);
        if (account==null){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NULL);
        }
        if (account.getCertifiedAccountState()==StateData.CERTIFIED_STATE_ENABLE){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_ENABLE);
        }
        if (account.getCertifiedAccountState()==StateData.CERTIFIED_STATE_DELETE){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_DELETE);
        }
        account.setCertifiedAccountState(StateData.CERTIFIED_STATE_ENABLE);
        certifiedAccountMapper.updateById(account);
        //消息队列修改日志信息
        account.setCertifiedAccountUpdatePerson(platformId.toString());
        account.setCertifiedAccountUpdateTime(LocalDateTime.now());
        HashMap<String, Object> map = new HashMap<>();
        rabbitTemplate.convertAndSend("upCertifiedDate",map);
    }

    /***
     * 平台删除物业账号
     * @param certifiedAccountId
     * @param platformId
     */
    @Override
    public void deleteCertifiedState(Integer certifiedAccountId, Integer platformId) {
        CertifiedAccount account = certifiedAccountMapper.selectById(certifiedAccountId);
        if (account==null){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NULL);
        }
        if (account.getCertifiedAccountState()==StateData.CERTIFIED_STATE_DELETE){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_DELETE);
        }
        account.setCertifiedAccountState(StateData.CERTIFIED_STATE_DELETE);
        certifiedAccountMapper.updateById(account);
//        //消息队列修改日志信息
//        account.setCertifiedAccountUpdatePerson(platformId.toString());
//        account.setCertifiedAccountUpdateTime(LocalDateTime.now());
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("certifiedAccount",account);
//        rabbitTemplate.convertAndSend("upCertifiedDate",map);
        QueryWrapper<CertifiedInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("certified_account_id",certifiedAccountId);
        CertifiedInfo certifiedInfo = certifiedInfoMapper.selectOne(wrapper);
        Result<Boolean> result = stallClient.platformSetStallDown(certifiedInfo.getCertifiedInfoId());
        if (result.getCode()==500){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_ERROR);
        }
        if (result.getCode()!=200){
            throw new CertifiedException(result.getMessage(),result.getCode());
        }

    }

    /***
     * 平台禁用物业账号
     * @param certifiedAccountId
     * @param platformId
     */
    @Override
    public void disableCertifiedState(Integer certifiedAccountId, Integer platformId) {
        CertifiedAccount account = certifiedAccountMapper.selectById(certifiedAccountId);
        if (account==null){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NULL);
        }
        if (account.getCertifiedAccountState()==StateData.CERTIFIED_STATE_DISABLE){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_DISABLE);
        }
        account.setCertifiedAccountState(StateData.CERTIFIED_STATE_DISABLE);
        certifiedAccountMapper.updateById(account);
//        //消息队列修改日志信息
//        account.setCertifiedAccountUpdatePerson(platformId.toString());
//        account.setCertifiedAccountUpdateTime(LocalDateTime.now());
//        HashMap<String, Object> map = new HashMap<>();
//        rabbitTemplate.convertAndSend("upCertifiedDate",map);
        QueryWrapper<CertifiedInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("certified_account_id",certifiedAccountId);
        CertifiedInfo certifiedInfo = certifiedInfoMapper.selectOne(wrapper);
        System.out.println(certifiedInfo+"6666666666666666666");
        Result<Boolean> result = stallClient.platformSetStallDown(certifiedInfo.getCertifiedInfoId());
        if (result.getCode()==500){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_ERROR);
        }
        if (result.getCode()!=200){
            throw new CertifiedException(result.getMessage(),result.getCode());
        }
    }
    /**
     * 根据ID查询物业账号
     * 参数: 物业账号Id certifiedAccountId
     * 返回: 物业账号Id certifiedAccountId
     *      物业账号 certifiedAccountAccount
     * 方法: 1.判断certifiedAccountId是否存在对应的物业账号 若不存在则抛异常
     *      2.判断对应的物业账号的状态是否是启用的 若不是则抛异常
     *      3.将物业账号放入Dto返回
     * */
    @Override
    public CertifiedAccountClientDto selectCertifiedAccountById(Integer certifiedAccountId) {
        CertifiedAccount certifiedAccount = certifiedAccountMapper.selectById(certifiedAccountId);
        if (certifiedAccount==null){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NULL);
        }
        if (certifiedAccount.getCertifiedAccountState()!=StateData.CERTIFIED_STATE_ENABLE){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_STATE_NOT_ENABLE);
        }
        CertifiedAccountClientDto dto = BeanUtil.toBean(certifiedAccount, CertifiedAccountClientDto.class);

        CertifiedInfo certifiedInfo = certifiedInfoMapper.selectOne(new QueryWrapper<CertifiedInfo>().eq("certified_account_id", dto.getCertifiedAccountId()));
        dto.setCertifiedInfoId(certifiedInfo.getCertifiedInfoId());
        return dto;
    }

    @Override
    public CertifiedDto findCertifiedInfoById(Integer certifiedInfoId) {
        CertifiedInfo certifiedInfo = certifiedInfoMapper.selectById(certifiedInfoId);
        CertifiedDto dto = BeanUtil.toBean(certifiedInfo, CertifiedDto.class);
        return dto;
    }

}
